/* 
    智能重复
    将2[1[a]3[b]2[3[c]4[d]]] 变成''

*/

const isLetter = (str) => {
    return str >= 'a' && str <= 'z'
}
const isNumber = (num) => {
    return num >= 0 && num <= 9
}
var smartRepeat = (str) => {
    const numStack = [], strStack = []
    for (let i = 0; i < str.length; i++) {
        if (isNumber(str[i])) {
            numStack.push(str[i])
            strStack.push("")
            i++
        } else if (isLetter(str[i])) {
            strStack[strStack.length - 1] += str[i]
        } else if (str[i] === "]") {
            const num = numStack.pop()
            const _str = strStack.pop()
            let temp = ""
            for (let j = 0; j < num; j++) {
                temp += _str
            }
            if (strStack.length) {
                strStack[strStack.length - 1] += temp
            } else {
                strStack.push(temp)
            }

        }
    }
    return strStack.pop()
}

const result = smartRepeat('2[1[a]3[b]2[3[c]4[d]]]')
console.log('smartRepeat :>>', result);